
//
//  ArroundTravelViewController.m
//  GoShops
//
//  Created by zcj on 15-12-6.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "ArroundTravelViewController.h"
#import "Grouble.h"
#import "MJRefresh.h"
#import "GetPlistArray.h"
#import "SelectFindHttp.h"
#import "GetUrlString.h"
#import "ArroundModel.h"
#import "ArroundTableViewCell.h"
#import "ArroundSectiondTableViewCell.h"
#import "ArroundFirstTableViewCell.h"


@interface ArroundTravelViewController ()<MJRefreshBaseViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView * myTableView;

@property (nonatomic, strong)MJRefreshFooterView *foot;
@property (nonatomic, strong)MJRefreshHeaderView *head;
@property (nonatomic,strong)NSArray * homeCity;
@property (nonatomic,strong)NSMutableArray * ArroundArray;
@property (nonatomic,strong)NSArray * AllCity;
@end

@implementation ArroundTravelViewController
- (NSArray *)AllCity
{
    if (_AllCity == nil) {
        _AllCity = [GetPlistArray arrayWithString:@"city.plist"];
    }
    return _AllCity;
}
- (NSMutableArray *)ArroundArray
{
    if (_ArroundArray == nil) {
        _ArroundArray = [NSMutableArray array];
    }
    return _ArroundArray;
}
- (NSArray *)homeCity
{
    if (_homeCity == nil) {
        _homeCity = [GetPlistArray arrayWithString:@"arroundcity.plist"];
    }
    return _homeCity;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavs];
    [self setTables];
    [self setRush];
    ZCJLog(@"%d",self.homeCity.count);
//    self.menuArray =  
    // Do any additional setup after loading the view.
}
- (void)setTables
{
    // 添加一个tableview
//    self.myTableView = [UITableView initWithTableView:CGRectMake(0, 0, ScreenWidth, ScreenHeight) withDelegate:self];
//    [self.view addSubview:self.myTableView];

    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];

}
-(void)setNavs
{
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    searchBar.backgroundImage = [UIImage imageNamed:@"icon_homepage_search"];
    searchBar.placeholder = @"搜索";
    self.navigationItem.titleView = searchBar;
    
    
    UIButton *MapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    MapBtn.frame = CGRectMake(0, 0, 50, 35);
    [MapBtn setImage:[UIImage imageNamed:@"icon_homepage_map_old"] forState:UIControlStateNormal];
    [MapBtn addTarget:self action:@selector(mapBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:MapBtn];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"searchList_btn_delete_6P"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)mapBtnClick
{
    ZCJLog(@"mapBtnClickArroundTravelViewController.h");
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.myTableView;
    head.delegate = self;
    [head beginRefreshing];
    self.head = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.myTableView;
    foot.delegate = self;
    self.foot = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    [self.foot endRefreshing];
    
}
-(void)loadNewData
{
    [self loadMerchantData];
}
// 加载数据
- (void)loadMerchantData
{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString * urlWithStr = [[GetUrlString sharedManager]urlWithHomeArround];
        
        [SelectFindHttp getWirhUrl:urlWithStr parms:nil success:^(id json) {
            NSArray * dataArray = json[@"data"];
            [self.ArroundArray removeAllObjects];
            for (NSDictionary * dict in dataArray) {
                ArroundModel * arroundModel = [ArroundModel objectWithKeyValues:dict];
                [self.ArroundArray addObject:arroundModel];
            }
            ZCJLog(@"%d",self.ArroundArray.count);
            [self.head endRefreshing];
            [self.myTableView reloadData];
        } failture:^(id error) {
            
        }];
    });
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2 + self.ArroundArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ArroundFirstTableViewCell * cell = [ArroundFirstTableViewCell ArroundFirstTableViewCell:tableView imgWithTitle:self.homeCity];
        return cell;
    }else if (indexPath.row == 1){
        ArroundSectiondTableViewCell * cell = [ArroundSectiondTableViewCell ArroundSectiondTableViewCell:tableView cityArray:self.AllCity];
        return cell;
    }else{
        ArroundTableViewCell * cell = [ArroundTableViewCell ArroundTableViewCell:tableView];
        cell.arroundModel = self.ArroundArray[indexPath.row - 2];
        return cell;
    }
    return nil;
}
- (void)dealloc
{
    [self.foot free];
    [self.head free];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 240;
    }else if (indexPath.row == 1){
        return 160;
    }
    return 140;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
